package pl.edu.agh.ssm.monitor.parsers.rtsp;

import pl.edu.agh.ssm.monitor.data.RTSPAnswer;
import pl.edu.agh.ssm.monitor.data.SessionDescription;
import pl.edu.agh.ssm.monitor.parsers.sdp.InvalidDataException;
import pl.edu.agh.ssm.monitor.parsers.sdp.SDPParser;

public class RTSPAnswerParser {

	public RTSPAnswer parse(byte[] data) {
		try {
			String str = new String(data);
			if (str.startsWith("RTSP")) {
				// String answerText = new String(data);
				// System.out.println("Try to parse answer:");
				// System.out.println(answerText);

				RTSPAnswer answer = new RTSPAnswer();
				String[] tmp = str.split("\n");
				// parse first line
				String[] fLine = tmp[0].split(" ");
				answer.setProtocol(fLine[0]);
				answer.setAnswerCode(Integer.valueOf(fLine[1].trim())
						.intValue());
				answer.setAnswerText(fLine[2]);
				answer.setFullPacket(str);

				for (int i = 1; i < tmp.length; i++) {
					// System.out.println("Try to parse line: " + tmp[i] +
					// "LLength: " + tmp[i].length());
					// if(tmp[i].length() == 1) {
					// System.out.println(Character.getNumericValue(tmp[i].charAt(0)));
					// }
					if (tmp[i].startsWith("Cseq")) {
//						System.out.println(tmp[i]);
						answer.setCSeq(Long.valueOf(
								tmp[i].split(":", 2)[1].trim()).longValue());
						continue;
					}
					if (tmp[i].startsWith("Session")) {
						answer.setSession(Long.valueOf(
								tmp[i].split(":", 2)[1].trim()).longValue());
						continue;
					}
					if (tmp[i].startsWith("v=")) {
						// if(parseLine(tmp[i], answer) && i + 1 < tmp.length) {
						// System.out.println("Parsing returned true");
						StringBuilder builder = new StringBuilder();
						for (int j = i; j < tmp.length; j++) {
							builder.append(tmp[j]);
							builder.append("\n");
						}
						// System.out.println("Try to parse SDP:\n" +
						// builder.toString());
						SDPParser parser = new SDPParser();
						try {
							SessionDescription desc = parser.parse(builder
									.toString());
							answer.setSdp(desc);
						} catch (InvalidDataException e) {
							e.printStackTrace();
						}
						break;
					}
				}
				return answer;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param line
	 * @param answer
	 * @return true if empty line was found - potentially next line is start of
	 *         SDP
	 */
//	private boolean parseLine(String line, RTSPAnswer answer) {
//		String[] tmp = line.split(":", 2);
//		if (tmp.length == 2) {
//			if (tmp[0].trim().equalsIgnoreCase("cseq")) {
//				answer.setCSeq(Long.valueOf(tmp[1].trim()).longValue());
//				return false;
//			}
//			if (tmp[0].trim().equalsIgnoreCase("session")) {
//				answer.setSession(Long.valueOf(tmp[1].trim()).longValue());
//				return false;
//			}
//		} else {
//			if (Character.getNumericValue(tmp[0].charAt(0)) == -1) { // empty
//																		// line
//				return true;
//			}
//		}
//		return false;
//	}

}
